<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .list{
            height: 150px;
            overflow-y: auto;
            width: 100px;
        }
        .news{
            height: 30px;
        }
        input:checked{
        background-color: #5290ff;
        }
    </style>
</head>
<body>
    <div id="test"></div>
    <script src="../lib2/react.development.js"></script>
    <!-- //引入核心库 -->
    <script src="../lib2//react-dom.development.js"></script>
    <!-- //支持react的dom操作 -->
    <script src="../lib/babel.min.js"></script>
    <!-- lib2中的babel版本太高了 -->
    <script type="text/babel">
        class MyComponent extends React.Component{
           static getDerivedStateFromProps(props,state){
               //该生命周期基本不用
               const {a}=state
                console.log('getDerivedStateFormProps,需要加static表示类的实例',props,state)
                //返回一个state的值后更改里面的值数据也不会改变(替代了以前componentwillreceive)
                return {a:2}
                //可以返回null不做手脚(必须返回state对象)
            }
            constructor(){
                super()
                this.state={a:1,newArr:[]}
                console.log('constructor')
            }
            add=()=>{
                const {a}=this.state
                this.setState({a:'23'})
            }
            getSnapshotBeforeUpdate(){
                return this.refs.list.scrollHeight//返回当前(更新前的)的滚轴高度
            }
            componentDidUpdate(preProps,preState,height){
                console.log(preProps,preState)
                this.refs.list.scrollTop+=this.refs.list.scrollHeight-height
                //继续增加,但是停留在当前位置
            }
            componentDidMount(){
                setInterval(()=>{
                    const {newArr}=this.state
                    const news='新闻'+(newArr.length+1)
                    this.setState({newArr:[news,...newArr]})
                },1000)
            }
            render(){
                return(
                    <div>
                        <button onClick={this.add}>点我加1</button>
                        <input type="checkbox"/>
                        <div className="list" ref='list'>
                            {
                                this.state.newArr.map((n,index)=>{
                                    return <div className="news" key={index}>{n}</div>
                                })
                            }
                          </div>
                        </div>
                )
            }
        }
        ReactDOM.render(<MyComponent name='邹'/>,document.getElementById('test'))
        </script>
</body>
</html>
